var treeData;

$(document).ready(function () {
    searchflglxx();
    insertSjjfl();
});

function searchflglxx() {
    $.ajax({
        type:"post",
        dataType:"json",
        url: "classification/queryDataSetClassification",
        success:function(result)
        {
            //所有数据集数据
            treeData=result;
            InitTreeInfo(treeData);
            InitData();
        }
    });
}

//初始化清空右边填写的内容
function InitData(){
    var flOpt= $("#fljb");
    var yjOpt=$("#yjlbdm");
    var ejOpt=$("#ejlbdm");
    var fldmObj=$("#fldm");
    var flmcObj=$("#flmc");

    flOpt.val("1");
    fldmObj.val('');
    flmcObj.val('');

    yjOpt.empty();
    ejOpt.empty();
    flOpt.attr('disabled', false);
    yjOpt.attr('disabled', true);
    ejOpt.attr('disabled', true);
}

//初始化树
function InitTreeInfo(treeData){
    arr=[];
    $.each(treeData,function(index,treeObj){
        var tempJson={};
        tempJson=treeObj;
        if(treeObj.FLJB=='2'){
            tempJson["pId"]=treeObj.YJLBDM;
        }
        if(treeObj.FLJB=='3')
        {
            tempJson["pId"]=treeObj.EJLBDM;
        }
        tempJson.id=treeObj.SJJFLDM;
        tempJson.name=treeObj.SJJFLMC;
        arr.push(tempJson);
    });
    InitTreeList(arr);
}

/**
 * 建立节点树列表
 * @returns
 */
function InitTreeList(zTreeNodes)
{
    //初始化树
    $.fn.zTree.init($("#dataTree"), setting, zTreeNodes);

    //添加表头
    var li_head = ' <li class="head"><a>' +
        '<div class="divTd" style="width: 45%;text-align: left">分类名称</div>' +
        '<div class="divTd" style="width:45%;text-align: left">分类代码</div>' +
        '<div class="divTd" style="width:10%;text-align: center"">操作</div></a></li>';

    var rows = $("#dataTree").find('li');
    if (rows.length > 0) {
        rows.eq(0).before(li_head)
    } else {
        $("#dataTree").append(li_head);
        $("#dataTree").append('<li ><div style="text-align: center;line-height: 30px;" >无符合条件数据</div></li>')
    }
    ;

    var treeObj = $.fn.zTree.getZTreeObj("dataTree");
    var nodes = treeObj.getNodes();

    if (nodes.length > 0) {
        //默认展开一级节点
        //treeObj.expandNode(nodes[0], true, false, true);
        treeObj.expandAll(true);
        //默认选中根节点
        treeObj.selectNode(nodes[0]);
    }
}


var setting = {
    view: {
        showLine: false,
        addDiyDom: addDiyDom,
        showIcon: false
    },
    data: {
        simpleData: {
            enable: true
        }
    },
    callback : {
        // 单击事件
        onClick : zTreeOnClick,
    }
};

//树节点单击事件
function  zTreeOnClick(event, treeId, treeNode){
    InitData();
    //标志 改为 修改
    $("#add_flag").val(false);

    var  tempFljb=treeNode.FLJB;
     $("#fljb").val(tempFljb);
    var yjOpt= $("#yjlbdm") ;
    var yjlbdm=treeNode.YJLBDM;
    var ejOpt= $("#ejlbdm");
    var ejlbdm=treeNode.EJLBDM;

     $("#fldm").val(treeNode.SJJFLDM);
     $("#flmc").val(treeNode.SJJFLMC);

    if(tempFljb =='1') {
        yjOpt.attr('disabled', true);
        ejOpt.attr('disabled', true);
    }

    if(tempFljb =='2' || tempFljb =='3') {
        var optParam = '<option value="0" >请选择...</option>';
        yjOpt.append(optParam);
        $.each(treeData, function (index, treeNode) {
            if (treeNode.FLJB == '1') {
                optParam = '<option  value=' + treeNode.SJJFLDM + '>|--' + treeNode.SJJFLMC + '</option>';
                yjOpt.append(optParam);
            }
        });
        yjOpt.find("option[value = '"+yjlbdm+"']").attr("selected","selected");
        yjOpt.attr('disabled', false);
    }
    if (tempFljb == '3') {
        var optParam='<option value="0" >请选择...</option>';
        ejOpt.append(optParam);
        var tempYj=yjOpt.val();
        $.each(treeData,function(index,treeNode){
            if(treeNode.pId==tempYj){
                 optParam='<option  value='+treeNode.SJJFLDM+'>|--'+treeNode.SJJFLMC+'</option>';
                ejOpt.append(optParam);
            }
        });
        var size =ejOpt.find("option").length;
        if(size==1){
            $("#ejlbdm option[value='0']").remove();
        }
        ejOpt.find("option[value = '"+ejlbdm+"']").attr("selected","selected");
        yjOpt.attr('disabled', false);
        ejOpt.attr('disabled', false);
    }
    $("#fljb").attr('disabled', true);

}

/**
 * 自定义DOM节点
 */
function addDiyDom(treeId, treeNode) {
    var spaceWidth = 15;
    var liObj = $("#" + treeNode.tId);
    var aObj = $("#" + treeNode.tId + "_a");
    var switchObj = $("#" + treeNode.tId + "_switch");
    var icoObj = $("#" + treeNode.tId + "_ico");
    var spanObj = $("#" + treeNode.tId + "_span");
    aObj.attr('title', '');
    aObj.append('<div class="divTd swich fnt" style="width: 45%"></div>');
    var div = $(liObj).find('div').eq(0);
    //从默认的位置移除
    switchObj.remove();
    spanObj.remove();
    icoObj.remove();
    //在指定的div中添加
    div.append(switchObj);
    spanObj.css("float", "left");
    div.append(spanObj);
    //隐藏了层次的span
    var spaceStr = "<span style='height:1px;display: inline-block;width:" + (spaceWidth * treeNode.level) + "px'></span>";
    switchObj.before(spaceStr);
    //图标垂直居中
    icoObj.css("margin-top", "9px");
    switchObj.after(icoObj);
    var editStr = '';
    //宽度需要和表头保持一致
    editStr += '<div class="divTd" style="width:45%;text-align: left">' + (treeNode.SJJFLDM == null ? '' : treeNode.SJJFLDM ) + '</div>';
    editStr += '<div class="divTd" style="width:10%;text-align: center">' + opt(treeNode) + '</div>';
    aObj.append(editStr);
}

//操作图标
function opt(treeNode) {
    var htmlStr = '';
    htmlStr += '<div>' +
        '<a class="red" href="#" onclick="doDelete(\'' + treeNode.SJJFLDM + '\')">' +
        '<i class="btn-del-wn" style="font-size: 125%" title="删除"></i></a></div>'
    return htmlStr;
}

//新增
function insertSjjfl(){
    $("#add_flag").val(true);
    var flOpt= $("#fljb");
    var yjOpt=$("#yjlbdm");
    var ejOpt=$("#ejlbdm");
    var fldmObj=$("#fldm");
    var flmcObj=$("#flmc");

    flOpt.val("1");
    yjOpt.val("0");
    ejOpt.val("0");
    fldmObj.val('');
    flmcObj.val('');
    flOpt.attr('disabled', false);
    yjOpt.attr('disabled', true);
    ejOpt.attr('disabled', true);

    yjOpt.empty();
    ejOpt.empty();

    flOpt.change(function(){
        var tempFljb=flOpt.val();

        var yjOpt=$("#yjlbdm");
        yjOpt.empty();
        var ejOpt=$("#ejlbdm");
        ejOpt.empty();

        if(tempFljb =='1' ) {
            yjOpt.attr('disabled', true);
            ejOpt.attr('disabled', true);
        }

        if(tempFljb =='2' || tempFljb =='3'){
            var optParam='<option value="0" >请选择...</option>';
            yjOpt.append(optParam);
            $.each(treeData,function(index,treeNode){
                if(treeNode.FLJB=='1'){
                     optParam='<option  value='+treeNode.SJJFLDM+'>|--'+treeNode.SJJFLMC+'</option>';
                    yjOpt.append(optParam);
                }
            });
            yjOpt.attr('disabled', false);
            ejOpt.attr('disabled', true);
        }
        if(tempFljb =='3' ){
            yjOpt.attr('disabled', false);
            ejOpt.attr('disabled', false);
        }
    });

    yjOpt.change(function(){
        if(flOpt.val()!='2'){
            ejOpt.empty();
            var optParam='<option value="0" >请选择...</option>';
            ejOpt.append(optParam);
            var tempYj=yjOpt.val();
            $.each(treeData,function(index,treeNode){
                if(treeNode.pId==tempYj){
                     optParam='<option  value='+treeNode.SJJFLDM+'>|--'+treeNode.SJJFLMC+'</option>';
                    ejOpt.append(optParam);
                }
            });
            var size =ejOpt.find("option").length;
            if(size==1){
                $("#ejlbdm option[value='0']").remove();
            }
        }
    });

}

//保存
function saveSjjfl() {
    var fljb=$("#fljb").val();
    var sjjfldm=$("#fldm").val();
    var sjjflmc=$("#flmc").val();

    var yjlbdm=$("#yjlbdm").val();
    var ejlbdm=$("#ejlbdm").val();
    var addFlag=$("#add_flag").val();

    if(fljb=='0'){
        LayerWn.showSucAlert('请选择分类级别！');
        return;
    }
    if(sjjflmc==''||sjjflmc==null){
        LayerWn.showSucAlert("类别名称不允许为空！");
        return;
    }
    // else if(sjjfldm==''||sjjfldm==null){
    //     LayerWn.showSucAlert("类别代码不允许为空！");
    //     return;
    // }

    var data={fljb:fljb,yjlbdm:yjlbdm,ejlbdm:ejlbdm,sjjfldm:sjjfldm,sjjflmc:sjjflmc};

    //addFlag用来区分新增还是更新
    var url="classification/saveDataSetClassification";
    if("false"==addFlag){
        url="classification/updateDataSetClassification";
    }
    $.ajax({
        type:"post",
        contentType: 'application/json',
        dataType:"json",
        data:JSON.stringify(data),
        url:url,
        success:function(result)
        {
            if (result.code == 200) {
                if (result.res == "1") {
                    LayerWn.showSucAlert('保存成功！');
                    setTimeout(function () {
                        parent.layer.closeAll()
                    }, 2000);
                    searchflglxx();
                } else if (result.res == "-1") {
                    LayerWn.showSucAlert('已存在！');
                }else if (result.res == "2") {
                    LayerWn.showSucAlert('更新成功！');
                    setTimeout(function () {
                        parent.layer.closeAll()
                    }, 2000);
                    searchflglxx();
                }
            } else {
                LayerWn.showFailAlert('服务器异常,请联系管理员');
            }
        }
    });

}

//删除
function doDelete(sjjfldm){
    LayerWn.showConfirm("确定要删除吗？", function () {
        $.ajax({
            type: "GET",
            contentType: 'application/json',
            dataType: "json",
            data:{sjjfldm:sjjfldm},
            url: "classification/deleteDataSetClassification",
            success: function (jsonData) {
                if (jsonData.code == 200) {
                    LayerWn.showSucAlert("删除成功");
                    searchflglxx();
                }else {
                    LayerWn.showFailAlert('服务器异常,请联系管理员');
                }
            }
        });
    });
}






